﻿//=============================================================
//  Copyright (C) 2016-2100
//  CLR版本:                	4.0.30319.42000
//  机器名称:               	YPFCOMPUTER
//  命名空间名称/文件名:    	SelfHostWebApi/ImageHelper 
//  创建人:			   	  		YPF     
//  创建时间:     		  		2017/5/9 11:23:23
//  网站：				  		
//==============================================================
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using VideoEncoder;

namespace SelfHostWebApi
{
    public class ImageHelper
    {
        /// <summary>
        /// 判断目录是否存在
        /// </summary>
        /// <param name="path">指定目录</param>
        private void DirectoryExists(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 获取视频信息
        /// </summary>
        /// <param name="oriVideoPath">视频全路径</param>
        /// <param name="totalTime">视频时长</param>
        /// <returns></returns>
        public VideoFile GetVideoInfo(string oriVideoPath, out string totalTime)
        {
            VideoEncoder.Encoder enc = new VideoEncoder.Encoder();
            string ffmpegPath = "ffmpeg.exe";
            enc.FFmpegPath = ffmpegPath;
            //视频路径
            string videoFilePath = oriVideoPath;
            VideoFile videoFile = new VideoFile(videoFilePath);

            enc.GetVideoInfo(videoFile);

            TimeSpan totaotp = videoFile.Duration;
            totalTime = string.Format("{0:00}:{1:00}:{2:00}", (int)totaotp.TotalHours, totaotp.Minutes, totaotp.Seconds);
            return videoFile;
        }

        /// <summary>
        /// 生成截图
        /// </summary>
        /// <param name="uploadDir">服务器的Upload 文件夹 物理地址</param>
        /// <param name="oriVideoPath">视频全路径</param>
        /// <param name="numberOfFrames">帧数</param>
        /// <param name="width">截图宽</param>
        /// <param name="height">截图高</param>
        /// <returns>截图全路径</returns>
        public string CatchThumb(string uploadDir, string oriVideoPath, int numberOfFrames = 10, int width = 521, int height = 521)

        {
            string result = "";
            DirectoryExists(uploadDir);
            //图片生成地址
            string uploadDirImg = uploadDir + @"\Images";
            DirectoryExists(uploadDirImg);
            try
            {
                FileInfo video = new FileInfo(oriVideoPath);
                if (video.Exists)
                {
                    string ext = video.Extension;

                    string ffmpegPath = "ffmpeg.exe";
                    FileInfo mp = new FileInfo(ffmpegPath);
                    if (mp.Exists)
                    {
                        string thubImagePath = uploadDirImg + @"\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
                        string command = string.Format("-ss {1} -i {0} -vframes 1 -r 1 -ac 1 -ab 2 -s {2}x{3} -f image2 {4}", oriVideoPath, numberOfFrames, width, height, thubImagePath);
                        var p = Process.Start(ffmpegPath, command); p.Close();
                        Thread.Sleep(2000);
                        FileInfo info = new FileInfo(thubImagePath);
                        if (info.Exists)
                        {
                            result = info.FullName;
                        }
                    }
                }
                else
                {
                    result = "视频文件不存在";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return result;
        }
    }
}
